﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace YmSoft.Kblog.Utility
{
    /// <summary>
    /// SQLHelper数据库助手类For MS SQLServer
    /// </summary>
    public abstract class SQLHelper
    {
        private static readonly string SQLServer2k8 = ConfigurationManager.ConnectionStrings["ConnectSQL"].ConnectionString.ToString();
        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="SqlStr"></param>
        /// <param name="CmdType"></param>
        /// <param name="Parms"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string SqlStr, CommandType CmdType, SqlParameter[] Parms)
        {
            SqlCommand Cmd = new SqlCommand();
            using (SqlConnection Conn = new SqlConnection(SQLServer2k8))
            {
                PrepareCommand(SqlStr, Conn, Cmd, CmdType, Parms);
                int val = Cmd.ExecuteNonQuery();
                Cmd.Parameters.Clear();
                return val;
            }
        }
        public static int ExecuteNonQuery(string SqlStr)
        {
            SqlCommand Cmd = new SqlCommand();
            using (SqlConnection Conn = new SqlConnection(SQLServer2k8))
            {
                PrepareCommand(SqlStr, Conn, Cmd, CommandType.Text, null);
                int val = Cmd.ExecuteNonQuery();
                Cmd.Parameters.Clear();
                return val;
            }
        }

        /// <summary>
        /// 执行SQL查询并返回
        /// </summary>
        /// <param name="SqlStr"></param>
        /// <param name="CmdType"></param>
        /// <param name="Parms"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteNonReader(string SqlStr, CommandType CmdType, SqlParameter[] Parms)
        {
            SqlCommand Cmd = new SqlCommand();
            SqlConnection Conn = new SqlConnection(SQLServer2k8);
            PrepareCommand(SqlStr, Conn, Cmd, CmdType, Parms);
            SqlDataReader dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
            Cmd.Parameters.Clear();
            return dr;
        }
        public static SqlDataReader ExecuteNonReader(string SqlStr)
        {
            SqlCommand Cmd = new SqlCommand();
            SqlConnection Conn = new SqlConnection(SQLServer2k8);
            PrepareCommand(SqlStr, Conn, Cmd, CommandType.Text, null);
            SqlDataReader dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
            Cmd.Parameters.Clear();
            return dr;
        }

        /// <summary>
        /// 通过SQL查询获取DataSet
        /// </summary>
        /// <param name="SqlStr"></param>
        /// <returns></returns>
        public static DataSet ExecuteNonDataSet(string SqlStr)
        {
            DataSet ds = new DataSet();
            SqlConnection Conn = new SqlConnection(SQLServer2k8);
            if (Conn.State != ConnectionState.Open)
            {
                Conn.Open();
            }
            SqlDataAdapter da = new SqlDataAdapter(SqlStr, Conn);
            da.Fill(ds);
            return ds;
        }

        /// <summary>
        /// 根据SQL查询返回DataTable.伪
        /// </summary>
        /// <param name="SqlStr">nonono</param>
        /// <returns>DataTable</returns>
        public static DataTable ExecuteNonReDataTable(string SqlStr)
        {
            DataSet ds = new DataSet();
            SqlConnection Conn = new SqlConnection(SQLServer2k8);
            if (Conn.State != ConnectionState.Open)
            {
                Conn.Open();
            }
            SqlDataAdapter da = new SqlDataAdapter(SqlStr, Conn);
            da.Fill(ds);
            return ds.Tables[0];
        }
        /// <summary>
        /// 根据SQL查询返回DataTable.真
        /// </summary>
        /// <param name="SqlStr">ok</param>
        /// <returns>DataTable</returns>
        public static DataTable ExecuteNonDataTable(string SqlStr)
        {
            DataTable dt;
            SqlConnection Conn = new SqlConnection(SQLServer2k8);
            SqlCommand Cmd = new SqlCommand();
            PrepareCommand(SqlStr, Conn, Cmd, CommandType.Text, null);
            SqlDataReader DR = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
            dt = new DataTable();
            dt.Load(DR);
            Cmd.Parameters.Clear();
            return dt;
        }
        /// <summary>
        /// 根据SQL查询返回DataTable.真
        /// </summary>
        /// <param name="SqlStr">ok</param>
        /// <returns>DataTable</returns>
        public static DataTable ExecuteNonDataTable(string SqlStr, CommandType type, SqlParameter[] parms)
        {
            DataTable dt;
            SqlConnection Conn = new SqlConnection(SQLServer2k8);
            SqlCommand Cmd = new SqlCommand();
            PrepareCommand(SqlStr, Conn, Cmd, type, parms);
            SqlDataReader DR = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
            dt = new DataTable();
            dt.Load(DR);
            Cmd.Parameters.Clear();
            return dt;
        }

        /// <summary>
        /// 根据SQL查询返回一条记录 object
        /// </summary>
        /// <param name="SqlStr"></param>
        /// <returns></returns>
        public static object ExecuteNonScalar(string SqlStr)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection Conn = new SqlConnection(SQLServer2k8))
            {
                PrepareCommand(SqlStr, Conn, cmd, CommandType.Text, null);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return val;
            }
        }
        public static object ExecuteNonScalar(string SqlStr, CommandType type, SqlParameter[] parms)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection Conn = new SqlConnection(SQLServer2k8))
            {
                PrepareCommand(SqlStr, Conn, cmd, type, parms);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return val;
            }
        }
        /// <summary>
        /// 根据SQL获取数据适配器
        /// </summary>
        /// <param name="SqlStr"></param>
        /// <returns></returns>
        public static SqlDataAdapter ExecuteNonSqlDataAdapter(string SqlStr)
        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection Conn = new SqlConnection(SQLServer2k8);
            PrepareCommand(SqlStr, Conn, cmd, CommandType.Text, null);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            return da;

        }
        /// <summary>
        /// 预备命令
        /// </summary>
        /// <param name="SqlStr"></param>
        /// <param name="Conn"></param>
        /// <param name="Cmd"></param>
        /// <param name="CmdType"></param>
        /// <param name="Parms"></param>
        private static void PrepareCommand(string SqlStr, SqlConnection Conn, SqlCommand Cmd, CommandType CmdType, SqlParameter[] Parms)
        {
            if (Conn.State != ConnectionState.Open)
            {
                Conn.Open();
            }
            Cmd.Connection = Conn;
            Cmd.CommandText = SqlStr;
            Cmd.CommandType = CmdType;
            if (Parms != null)
            {
                foreach (SqlParameter Parm in Parms)
                {
                    Cmd.Parameters.Add(Parm);
                }
            }
        }
    }
}
